package com.ourteam.workspace.dao.impl;

import com.ourteam.workspace.dao.IWorkspaceServerDAO;
import com.ourteam.workspace.dao.WorkspaceServer;
import com.ourteam.workspace.dao.WorkspaceServerDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-11-30 18:42:34 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractWorkspaceServerDAOJdbcImpl extends AbstractSpringDAO
    implements IWorkspaceServerDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        WorkspaceId,
        ServerName,
        ServerType,
        ServerLogo,
        StartCmd,
        StopCmd,
        RestartCmd,
        ServerLocation,
        ServerUser,
        ServerPassword,
        Status,
        Remarks,
        DefaultServer
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.WorkspaceId,
        _DataObjectProperty.ServerName,
        _DataObjectProperty.ServerType,
        _DataObjectProperty.ServerLogo,
        _DataObjectProperty.StartCmd,
        _DataObjectProperty.StopCmd,
        _DataObjectProperty.RestartCmd,
        _DataObjectProperty.ServerLocation,
        _DataObjectProperty.ServerUser,
        _DataObjectProperty.ServerPassword,
        _DataObjectProperty.Status,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.DefaultServer
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<WorkspaceServer, WorkspaceServerDAOQueryBean> daoHelper;

  public AbstractWorkspaceServerDAOJdbcImpl() {
    super();
    daoHelper = new DataAccessObjectHelper<WorkspaceServer, WorkspaceServerDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aWorkspaceServer
   * @throws Exception
   */
  public boolean checkIsUnique(WorkspaceServer aWorkspaceServer) throws Exception {

    if (aWorkspaceServer == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aWorkspaceServer.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aWorkspaceServer
   * @throws Exception
   */
  public void insert(WorkspaceServer aWorkspaceServer) throws Exception {

    super.doInsert(aWorkspaceServer);
  }

  /**
   * Batch Insert Datas
   *
   * @param aWorkspaceServer
   * @throws Exception
   */
  public void batchInsert(WorkspaceServer[] aWorkspaceServers) throws Exception {

    super.doBatchInsert(aWorkspaceServers);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(WorkspaceServerDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(WorkspaceServer aWorkspaceServer) throws Exception {

    delete(aWorkspaceServer.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aWorkspaceServer
   * @throws Exception
   */
  public void update(WorkspaceServer aWorkspaceServer) throws Exception {

    if (aWorkspaceServer.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aWorkspaceServer);

    WorkspaceServer tempObj = findById(aWorkspaceServer.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aWorkspaceServer, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aWorkspaceServers[]
   * @throws Exception
   */
  public void batchUpdate(WorkspaceServer[] aWorkspaceServers) throws Exception {

    super.doBatchUpdate(aWorkspaceServers);
  }

  /**
   * Update Data By Condictions
   *
   * @param aWorkspaceServer
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(WorkspaceServer aWorkspaceServer, WorkspaceServerDAOQueryBean aQueryBean)
      throws Exception {

    super.doUpdateByQuery(aWorkspaceServer, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(WorkspaceServerDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(WorkspaceServer aWorkspaceServer) throws Exception {

    if (aWorkspaceServer.getDefaultServer() == null) {
      throw new Exception("The WorkspaceServer.defaultServer value is not allow NULL");
    }

    if (aWorkspaceServer.getServerName() == null) {
      throw new Exception("The WorkspaceServer.serverName value is not allow NULL");
    }

    if (aWorkspaceServer.getServerType() == null) {
      throw new Exception("The WorkspaceServer.serverType value is not allow NULL");
    }

    if (aWorkspaceServer.getServerLogo() == null) {
      throw new Exception("The WorkspaceServer.serverLogo value is not allow NULL");
    }

    if (aWorkspaceServer.getStartCmd() == null) {
      throw new Exception("The WorkspaceServer.startCmd value is not allow NULL");
    }

    if (aWorkspaceServer.getStopCmd() == null) {
      throw new Exception("The WorkspaceServer.stopCmd value is not allow NULL");
    }

    if (aWorkspaceServer.getRestartCmd() == null) {
      throw new Exception("The WorkspaceServer.restartCmd value is not allow NULL");
    }

    if (aWorkspaceServer.getServerLocation() == null) {
      throw new Exception("The WorkspaceServer.serverLocation value is not allow NULL");
    }

    if (aWorkspaceServer.getServerUser() == null) {
      throw new Exception("The WorkspaceServer.serverUser value is not allow NULL");
    }

    if (aWorkspaceServer.getServerPassword() == null) {
      throw new Exception("The WorkspaceServer.serverPassword value is not allow NULL");
    }

    if (aWorkspaceServer.getStatus() == null) {
      throw new Exception("The WorkspaceServer.status value is not allow NULL");
    }

    if (aWorkspaceServer.getRemarks() == null) {
      throw new Exception("The WorkspaceServer.remarks value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public WorkspaceServer findById(final long id) throws Exception {

    WorkspaceServerDAOQueryBean queryBean = new WorkspaceServerDAOQueryBean();

    WorkspaceServerDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    WorkspaceServer[] WorkspaceServers = query(queryBean);

    if (WorkspaceServers.length == 0) {
      return null;
    } else {
      return WorkspaceServers[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return WorkspaceServer[]
   * @throws Exception
   */
  public WorkspaceServer[] query(WorkspaceServerDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcWorkspaceServerRowMapper());

    return (WorkspaceServer[]) result.toArray(new WorkspaceServer[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(WorkspaceServerDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(WorkspaceServerDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      WorkspaceServer[] aWorkspaceServers, WorkspaceServerDAOQueryBean queryBean) throws Exception {
    daoHelper.synchronize(aWorkspaceServers, queryBean);
  }

  public void synchronize(
      WorkspaceServer[] aWorkspaceServers,
      WorkspaceServerDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<WorkspaceServer> callBack)
      throws Exception {
    daoHelper.synchronize(aWorkspaceServers, queryBean, callBack);
  }

  public static class JdbcWorkspaceServerRowMapper implements RowMapper<WorkspaceServer> {

    public WorkspaceServer mapRow(ResultSet resultSet, int arg1) throws SQLException {
      WorkspaceServer object = new WorkspaceServer();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object workspaceId = null;

        workspaceId = resultSet.getObject(columnIndex++);

        object.setWorkspaceIdNull(resultSet.getBoolean(columnIndex++));

        if (workspaceId != null) {

          setBeanValue(object, _DataObjectProperty.WorkspaceId, workspaceId);
        }

        Object serverName = null;

        serverName = resultSet.getObject(columnIndex++);

        object.setServerNameNull(resultSet.getBoolean(columnIndex++));

        if (serverName != null) {

          setBeanValue(object, _DataObjectProperty.ServerName, serverName);
        }

        Object serverType = null;

        serverType = resultSet.getObject(columnIndex++);

        object.setServerTypeNull(resultSet.getBoolean(columnIndex++));

        if (serverType != null) {

          setBeanValue(object, _DataObjectProperty.ServerType, serverType);
        }

        Object serverLogo = null;

        serverLogo = resultSet.getObject(columnIndex++);

        object.setServerLogoNull(resultSet.getBoolean(columnIndex++));

        if (serverLogo != null) {

          setBeanValue(object, _DataObjectProperty.ServerLogo, serverLogo);
        }

        Object startCmd = null;

        startCmd = resultSet.getObject(columnIndex++);

        object.setStartCmdNull(resultSet.getBoolean(columnIndex++));

        if (startCmd != null) {

          setBeanValue(object, _DataObjectProperty.StartCmd, startCmd);
        }

        Object stopCmd = null;

        stopCmd = resultSet.getObject(columnIndex++);

        object.setStopCmdNull(resultSet.getBoolean(columnIndex++));

        if (stopCmd != null) {

          setBeanValue(object, _DataObjectProperty.StopCmd, stopCmd);
        }

        Object restartCmd = null;

        restartCmd = resultSet.getObject(columnIndex++);

        object.setRestartCmdNull(resultSet.getBoolean(columnIndex++));

        if (restartCmd != null) {

          setBeanValue(object, _DataObjectProperty.RestartCmd, restartCmd);
        }

        Object serverLocation = null;

        serverLocation = resultSet.getObject(columnIndex++);

        object.setServerLocationNull(resultSet.getBoolean(columnIndex++));

        if (serverLocation != null) {

          setBeanValue(object, _DataObjectProperty.ServerLocation, serverLocation);
        }

        Object serverUser = null;

        serverUser = resultSet.getObject(columnIndex++);

        object.setServerUserNull(resultSet.getBoolean(columnIndex++));

        if (serverUser != null) {

          setBeanValue(object, _DataObjectProperty.ServerUser, serverUser);
        }

        Object serverPassword = null;

        serverPassword = resultSet.getObject(columnIndex++);

        object.setServerPasswordNull(resultSet.getBoolean(columnIndex++));

        if (serverPassword != null) {

          setBeanValue(object, _DataObjectProperty.ServerPassword, serverPassword);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object defaultServer = null;

        defaultServer = resultSet.getObject(columnIndex++);

        object.setDefaultServerNull(resultSet.getBoolean(columnIndex++));

        if (defaultServer != null) {

          setBeanValue(object, _DataObjectProperty.DefaultServer, defaultServer);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
